package com.lineage.server.datatables;

import com.lineage.DatabaseFactoryLogin;
import com.lineage.config.Config;
import com.lineage.server.IdFactory;
import com.lineage.server.datatables.lock.ClanEmblemReading;
import com.lineage.server.templates.DeClan;
import com.lineage.server.templates.L1EmblemIcon;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DeClanTable {
  private static final Log _log = LogFactory.getLog(DeClanTable.class);
  
  private static final Map<Integer, DeClan> _declanList = new HashMap<>();
  
  private static DeClanTable _instance;
  
  public static DeClanTable get() {
    if (_instance == null)
      _instance = new DeClanTable(); 
    return _instance;
  }
  
  public void load() {
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactoryLogin.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM `de_clan`");
      rs = pstm.executeQuery();
      while (rs.next()) {
        String clanname = "";
        if (Config.CLIENT_LANGUAGE == 3) {
          clanname = rs.getString("clannamebig5");
        } else {
          clanname = rs.getString("clanname");
        } 
        int clan_id = rs.getInt("clan_id");
        String leader_name = "";
        if (Config.CLIENT_LANGUAGE == 3) {
          leader_name = rs.getString("leader_namebig5");
        } else {
          leader_name = rs.getString("leader_name");
        } 
        int emblemid = rs.getInt("emblemid");
        DeClan deClan = new DeClan(clanname, clan_id, leader_name, emblemid);
        _declanList.put(Integer.valueOf(clan_id), deClan);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
  }
  
  public void loadIcon() {
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactoryLogin.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM `de_emblem_tmp`");
      rs = pstm.executeQuery();
      while (rs.next()) {
        int clanid = rs.getInt("clan_id");
        byte[] icon = rs.getBytes("emblemicon");
        int update = rs.getInt("update");
        int emblemid = rs.getInt("emblemid");
        if (emblemid == 0) {
          emblemid = IdFactory.get().nextId();
          updata_de_emblem(emblemid, clanid);
        } 
        L1EmblemIcon emblemIcon = new L1EmblemIcon();
        emblemIcon.set_clanid(clanid);
        emblemIcon.set_clanIcon(icon);
        emblemIcon.set_update(update);
        emblemIcon.set_emblemid(emblemid);
        updata_deClan(emblemIcon);
        ClanEmblemReading.get().addDeclanEmblem(clanid, emblemIcon);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
  }
  
  private void updata_deClan(L1EmblemIcon emblemIcon) {
    Connection cn = null;
    PreparedStatement ps = null;
    try {
      cn = DatabaseFactoryLogin.get().getConnection();
      ps = cn.prepareStatement("UPDATE `de_clan` SET `emblemid`=? WHERE `clan_id`=?");
      int i = 0;
      ps.setInt(++i, emblemIcon.get_emblemid());
      ps.setInt(++i, emblemIcon.get_clanid());
      ps.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
  
  private void updata_de_emblem(int emblemid, int clanid) {
    Connection cn = null;
    PreparedStatement ps = null;
    try {
      cn = DatabaseFactoryLogin.get().getConnection();
      ps = cn.prepareStatement("UPDATE `de_emblem_tmp` SET `emblemid`=? WHERE `clan_id`=?");
      int i = 0;
      ps.setInt(++i, emblemid);
      ps.setInt(++i, clanid);
      ps.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
  
  public DeClan get(int clanid) {
    return _declanList.get(Integer.valueOf(clanid));
  }
  
  public Collection<DeClan> getList() {
    return _declanList.values();
  }
}
